from typing import List

class Solution:
    def trap(self, height: List[int]) -> int:
        if not height:
            return 0
            
        n = len(height)
        # 创建两个数组来存储每个位置左右两边的最大高度
        left_max = [0] * n
        right_max = [0] * n
        
        # 计算每个位置左边的最大高度
        left_max[0] = height[0]
        for i in range(1, n):
            left_max[i] = max(height[i], left_max[i-1])
            
        # 计算每个位置右边的最大高度
        right_max[n-1] = height[n-1]
        for i in range(n-2, -1, -1):
            right_max[i] = max(height[i], right_max[i+1])
            
        # 计算每个位置能接的雨水
        water = 0
        for i in range(n):
            water += min(left_max[i], right_max[i]) - height[i]
            
        return water

# 测试代码
if __name__ == "__main__":
    solution = Solution()
    
    # 测试用例1
    height1 = [0,1,0,2,1,0,1,3,2,1,2,1]
    print(f"测试用例1结果: {solution.trap(height1)}")  # 预期输出: 6
    
    # 测试用例2
    height2 = [4,2,0,3,2,5]
    print(f"测试用例2结果: {solution.trap(height2)}")  # 预期输出: 9 